using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class MapManager : SingletonpatternMonoBase<MapManager>
{
    public GameObject bigGridPrefab;
    public GameObject smallGridPrefab;
    public BigGrid[,] bigGrids = new BigGrid[3, 3];
    public Vector2 gridSize;
    public Vector3 offset = new Vector3(3, 3, 0);
    void Start()
    {
        GenerateMap();
    }

  

    void GenerateMap()
    {
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                // Instantiate big grid
                GameObject bigGridObject = Instantiate(bigGridPrefab, new Vector3(i * gridSize.x, j * gridSize.y,0), Quaternion.identity);
                
                bigGrids[i, j] = bigGridObject.GetComponent<BigGrid>();

                for (int m = 0; m < 3; m++)
                {
                    for (int n = 0; n < 3; n++)
                    {
                        // Instantiate small grid
                        GameObject smallGridObject = Instantiate(smallGridPrefab, bigGridObject.transform);
                        smallGridObject.transform.localPosition = new Vector3((m-1) * (gridSize.x / 3), (n-1) * (gridSize.y /3f),0);
                        smallGridObject.GetComponent<SmallGrid>().gridState = GridState.Empty;

                        bigGrids[i, j].smallGrids[m, n] = smallGridObject.GetComponent<SmallGrid>();
                    }
                }
            }
        }
    }

    public GridState whoWin
    {
        get
        {
            // Check rows
            for (int i = 0; i < 3; i++)
            {
                if (bigGrids[i, 0].gridState == bigGrids[i, 1].gridState &&
                    bigGrids[i, 1].gridState == bigGrids[i, 2].gridState &&
                    bigGrids[i, 0].gridState != GridState.Empty)
                {
                    return bigGrids[i, 0].gridState;
                }
            }

            // Check columns
            for (int j = 0; j < 3; j++)
            {
                if (bigGrids[0, j].gridState == bigGrids[1, j].gridState &&
                    bigGrids[1, j].gridState == bigGrids[2, j].gridState &&
                    bigGrids[0, j].gridState != GridState.Empty)
                {
                    return bigGrids[0, j].gridState;
                }
            }

            // Check diagonals
            if (bigGrids[0, 0].gridState == bigGrids[1, 1].gridState &&
                bigGrids[1, 1].gridState == bigGrids[2, 2].gridState &&
               bigGrids[0, 0].gridState != GridState.Empty)
            {
                return bigGrids[0, 0].gridState;
            }

            if (bigGrids[0, 2].gridState == bigGrids[1, 1].gridState &&
                bigGrids[1, 1].gridState == bigGrids[2, 0].gridState &&
                bigGrids[0, 2].gridState != GridState.Empty)
            {
                return bigGrids[0, 2].gridState;
            }

            return GridState.Empty;
        }
    }
    public void CheckState()
    {
        if(whoWin!=GridState.Empty)
        {
            GetComponent<SpriteRenderer>().sprite = InputManager.Instance.soBigSprites[(int)InputManager.Instance.currentGridState];
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    bigGrids[i, j].HalfHideSmallGrid();
                }
            }
        }

    }
}

public enum GridState
{
    Empty,
    O,
    X,   
    Y
}